package com.tenton.service.impl;

import com.tenton.mapper.CommodityMapper;
import com.tenton.mapper.DailySalesMapper;
import com.tenton.pojo.Commodity;
import com.tenton.pojo.DailySales;
import com.tenton.service.DailySalesService;
import com.tenton.utils.Constants;
import com.tenton.utils.DateUtils;
import com.tenton.vo.DailySalesReqVo;
import com.tenton.vo.SalePolyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Date: 2021/3/30 11:18
 * @Author: Tenton
 * @Description:
 */
@Service
public class DailySalesServiceImpl implements DailySalesService {
    @Autowired
    DailySalesMapper dailySalesMapper;
    @Autowired
    CommodityMapper commodityMapper;

    @Override
    public Map<String,Object> polyDate() {
        //所有商品信息
        List<Commodity> commodityList = commodityMapper.selectAll();
        Map<String,Object> data = new HashMap<>();
        List<SalePolyVo> list = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        for (Commodity commodity : commodityList) {
            SalePolyVo vo = new SalePolyVo();
            vo.setName(commodity.getComName());
            vo.setType(Constants.POLY);
            List<Integer> numList = this.sevenDaySales(commodity.getComName());
            vo.setData(numList);
            list.add(vo);
            nameList.add(commodity.getComName());
        }
        List<String> dateList = this.sevenTime();
        //商品信息
        data.put(Constants.COMMODITY_KEY,list);
        //商品名称
        data.put(Constants.NAME_KEY,nameList);
        //销售时间
        data.put(Constants.TIME_KEY,dateList);
        return data;
    }

    /**
     * 获取销量当前日前七天销量
     *
     * @param comName
     */
    public List<Integer> sevenDaySales(String comName){
        //存储销量
        List<Integer> list = new ArrayList<>();
        //当天销售详细
        Date oneDate = new Date();
        int one = this.toDaySale(comName, oneDate);
        //一天前销售详细
        Date twoDate = DateUtils.getYesterday(oneDate);
        int two = this.toDaySale(comName, twoDate);
        //两天前销售详细
        Date threeDate = DateUtils.getYesterday(twoDate);
        int three = this.toDaySale(comName, threeDate);
        //三天前销售详细
        Date fourDate = DateUtils.getYesterday(threeDate);
        int four = this.toDaySale(comName, fourDate);
        //四天前销售详细
        Date fiveDate = DateUtils.getYesterday(fourDate);
        int five = this.toDaySale(comName, fiveDate);
        //五天前销售详细
        Date sixDate = DateUtils.getYesterday(fiveDate);
        int six = this.toDaySale(comName, sixDate);
        //六天前销售详细
        Date sevenDate = DateUtils.getYesterday(sixDate);
        int seven = this.toDaySale(comName, sevenDate);
        list.add(seven);
        list.add(six);
        list.add(five);
        list.add(four);
        list.add(three);
        list.add(two);
        list.add(one);
        return list;
    }

    /**
     * 近七天时间
     * @return
     */
    public List<String> sevenTime(){
        List<String> list = new ArrayList<>();
        Date one = new Date();
        Date two = DateUtils.getYesterday(one);
        Date three = DateUtils.getYesterday(two);
        Date four = DateUtils.getYesterday(three);
        Date five = DateUtils.getYesterday(four);
        Date six = DateUtils.getYesterday(five);
        Date seven = DateUtils.getYesterday(six);
        list.add(DateUtils.getYmdDate(seven));
        list.add(DateUtils.getYmdDate(six));
        list.add(DateUtils.getYmdDate(five));
        list.add(DateUtils.getYmdDate(four));
        list.add(DateUtils.getYmdDate(three));
        list.add(DateUtils.getYmdDate(two));
        list.add(DateUtils.getYmdDate(one));
        return list;
    }
    /**
     * 获取前一天商品日销量
     * @param comName
     * @param date
     */
    public int toDaySale(String comName, Date date){
        DailySalesReqVo vo = new DailySalesReqVo();
        //vo赋值
        vo.setComName(comName);
        vo.setSaleDate(date);
        //设置开始时间
        vo.setStartTime(DateUtils.setStartDate(date));
        //设置结束时间
        vo.setEndTime(DateUtils.setEndDate(date));
        DailySales dailySales = dailySalesMapper.selectByParam(vo);
        if (dailySales == null){
            return Constants.DAILY_SALE_NUM;
        }else {
            return dailySales.getNum();
        }
    }
}
